<template>
	<view class="person-container">
		<uni-search-bar placeholder="搜索" v-model="params.name" bgColor="#EEEEEE" @confirm="search" @input="search"
			@cancel="cancel" @clear="clear" />

		<!-- <uni-swipe-action>
			<uni-swipe-action-item v-for="(item,index) in personList" :right-options="options" :show="isOpened"
				:auto-close="true" @change="swipeChange($event, index)" @click="swipeClick($event, item)">
				<view class="content-box" @click="contentClick($event, item)">
					<text class="content-text">{{item.phoneNumber}} - {{item.name}}</text>
				</view>
			</uni-swipe-action-item>
		</uni-swipe-action> -->

		<uni-list>
			<uni-list-item v-for="(item,index) in personList" :title="item.name"
				:rightText="item.phoneNumber" link="navigateTo"
				:to="'/pages/package/work/person/detail?personId=' + item.personId"
				thumb="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png">
			</uni-list-item>
		</uni-list>



		<uni-fab ref="fab" :horizontal="horizontal" :vertical="vertical" :direction="direction" @fabClick="addClick" />
	</view>
</template>

<script>
	import {
		getPersonList,
		deletePerson
	} from '@/api/record/person.js'

	export default {
		data() {
			return {
				isOpened: "none",
				options: [{
						text: '详情',
						style: {
							backgroundColor: '#007aff'
						}
					},{
						text: '删除',
						style: {
							backgroundColor: '#F56C6C'
						}
					}
				],
				isToEnd: false,
				params: {
					name: '',
					pageNum: 1,
					pageSize: 13
				},
				horizontal: 'left',
				vertical: 'bottom',
				direction: 'horizontal',
				personList: [],
				avatarList: [{
					url: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png'
				}, {
					url: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png'
				}, {
					url: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png'
				}]
			}
		},
		onLoad() {
			
		},
		onShow(){
			this.getPerson()
		},
		onReachBottom() {
			if (this.isToEnd) {
				return
			}
			console.log(this.isToEnd)
			var index = this.params.pageNum
			this.params.pageNum = index + 1
			var that = this;
			var data = this.params
			getPersonList(data).then(response => {
				if (response.rows.length < that.params.pageSize) {
					that.isToEnd = true;
				}
				for (var item of response.rows) {
					that.personList.push(item)
				}

				console.log(that.personList)
			});
		},
		methods: {
			getPerson() {
				this.params.pageNum = 1;
				this.isToEnd = false;
				var data = this.params;
				console.log(data)
				getPersonList(data).then(response => {
					this.personList = response.rows;
				});
			},
			detailclick(item) {
				console.log("swipeClick:");
				console.log(item);
				uni.navigateTo({
					url: '/pages/package/work/person/detail?personId='+item.personId
				});
			},
			addClick() {
				uni.navigateTo({
					url: '/pages/package/work/person/add'
				});
			},
			clear() {
				console.log("clear")
				this.params.name = '';
			},
			cancel() {
				console.log("cancel")
				this.params.name = '';
			},
			search() {
				console.log("search")
				this.getPerson()
			},
			contentClick(e, item){
				uni.makePhoneCall({
					phoneNumber:item.phoneNumber,
					success(res) {
						console.log(res);
					},
					fail(err) {
						console.log(err);
						uni.showModal({
							title:"失败！"
						})
					},
					
				})
			},
			swipeChange(e,index){
				console.log('当前状态：'+ e +'，下标：' + index)
			},
			swipeClick(e, item) {
				console.log("swipeClick:");
				console.log(e);
				console.log('点击了'+(e.position === 'left' ? '左侧' : '右侧') + e.content.text + '按钮')
				console.log(item);
				var that = this;
				if(e.content.text=="详情"){
					uni.navigateTo({
						url: '/pages/package/work/person/detail?personId='+item.personId
					});
				} else if(e.content.text=="删除"){
					uni.showModal({
						title: '提示',
						content: '是否要删除该数据！',
						success: function (res) {
							if (res.confirm) {
								deletePerson(item.personId).then(response => {
									console.log(response)
									that.personList.forEach(function(it,index,arr){
										if(it.personId == item.personId){
											that.personList.splice(index,1);
											return;
										}
									});
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					
				}
			}
		}
	}
</script>

<style>
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	/* #endif */

	.chat-custom-right {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
	}

	.chat-custom-text {
		font-size: 12px;
		color: #999;
	}

	.content-box {
		flex: 1;
		/* #ifdef APP-NVUE */
		justify-content: center;
		/* #endif */
		height: 44px;
		line-height: 44px;
		padding: 0 15px;
		position: relative;
		background-color: #fff;
		border-bottom-color: #f5f5f5;
		border-bottom-width: 1px;
		border-bottom-style: solid;
	}

	.content-text {
		font-size: 15px;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding: 10px 0;
		background-color: #fff;
	}

	.button {
		border-color: #e5e5e5;
		border-style: solid;
		border-width: 1px;
		padding: 4px 8px;
		border-radius: 4px;
	}

	.button-text {
		font-size: 15px;
	}

	.slot-button {
		/* #ifndef APP-NVUE */
		display: flex;
		height: 100%;
		/* #endif */
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 0 20px;
		background-color: #ff5a5f;
	}

	.slot-button-text {
		color: #ffffff;
		font-size: 14px;
	}

	.box {
		border-radius: 50%;
		width: 30px;
		height: 30px;
		display: flex;
		justify-content: center;
		align-items: center;
		text-align: center;
		background-color: #b67bdf;
		color: #fff;
		font-size: 12px;
		margin-top: 6px;
	}

	.box-text {
		text-align: center;
		color: #fff;
		font-size: 18px;
		font-weight: bolder;
	}
</style>
